#!/bin/bash -e
# Copyright 2007 David Stone
# Distributed under the terms of the GNU General Public License v2
# http://builtbydave.co.uk

# Path to Policy file
XPAYPOLICY=/usr/local/xpay/policy

# Set the location of the XPay jar file
CLASSPATH=/usr/local/xpay/XPay.jar

# Path to java executable
JAVAPATH=/usr/bin/java

. /lib/lsb/init-functions

SELF=$(cd $(dirname $0); pwd -P)/$(basename $0)
DAEMON="$JAVAPATH -cp $CLASSPATH -Djava.security.manager -Djava.security.policy=$XPAYPOLICY XPay"
PIDFILE=/var/run/xpay.pid
LOGFILE=/var/log/xpay

sanity_checks() {
	# check pid doesn't exist.
	if [ -a $PIDFILE ]; then
		/bin/echo "ERROR: PID file $PIDFILE already exists."
		exit 1
	fi 
}

#
# main()
#

case "${1:-''}" in
	'start')
		sanity_checks
		log_begin_msg "Starting Xpay service..."
		$DAEMON &> $LOGFILE & echo $! > $PIDFILE
		log_end_msg $?
		;;
	'stop')
		log_begin_msg "Stopping Xpay service..."
		start-stop-daemon --stop --pidfile $PIDFILE
		rm $PIDFILE
		log_end_msg $?
		;;
	'restart')
		$SELF stop
		$SELF start 
		;;
  	*)
		/bin/echo "Usage: $SELF start|stop|restart"
		exit 1
		;;
esac

